<?php

namespace Admin\Controller;

/**
 * 艺术社群
 *
 * Class GroupController
 * @package Admin\Controller
 */
class GroupController extends PublicController
{
    private $model;

    public function _initialize()
    {
        $this->model = M('group');
        $this->assign('modules', '艺术社群');
    }

    /**
     * 艺术社群列表
     */
    public function index()
    {
        $groupType = intval($_GET['type']);
        if ($groupType == 2) {
            $this->assign('title', '志愿者招募');
        } else {
            $this->assign('title', '文艺团队招募');
        }
        $where = "is_del=0 and group_type= " . $groupType;
        if (isset($_GET['keyword'])) {
            $where .= " AND title like '%" . trim($_GET['keyword']) . "%'";
            $this->assign('keyword', trim($_GET['keyword']));
        }
        $count = $this->model->where($where)->count();
        $perPage = 20;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $totalPage = ceil($count / $perPage);
        $pageObj = $this->page_index($count, $totalPage, $page);
        $list = $this->model->where($where)->order('sort = 0, sort asc, id desc')->limit($page * $perPage, $perPage)->select();

        $this->assign('list', $list);
        $this->assign('pageObj', $pageObj);
        $this->assign('groupType', $groupType);
        $this->display('index');
    }

    /**
     * 文化日历编辑页面
     */
    public function add()
    {
        $groupType = intval($_GET['type']);

        if ($groupType == 2) {
            $this->assign('title', '发布志愿者招募');
        } else {
            $this->assign('title', '发布文艺团队招募');
        }

        if (isset($_GET['id'])) {

            $detail = $this->model->where(['id' => intval($_GET['id']), 'is_del' => 0])->find();
            if (!empty($detail)) {
                $groupType = $detail['group_type'];
                if ($groupType == 2) {
                    $this->assign('title', '修改志愿者招募信息');
                } else {
                    $this->assign('title', '修改文艺团队招募信息');
                }
                $this->assign("detail", $detail);
            }
        }
        $this->assign('groupType', $groupType);
        $this->display();
    }

    /**
     * 保存文化日历内容
     */
    public function save()
    {
        $postData = array(
            'title'      => $_POST['title'],
            'group_type' => intval($_POST['group_type']),
            'brief'      => $_POST['brief'],
            'content'    => $_POST['content'],
            'sort'       => intval($_POST['sort'])
        );

        // 封面图片
        if (!empty($_FILES["file"]["tmp_name"])) {
            $info = $this->upload_images($_FILES["file"], array('jpg', 'png', 'jpeg'), 'group/' . date('Ymd'));
            if (!is_array($info)) {
                $this->error($info);
            } else {
                $postData['cover'] = 'UploadFiles/' . $info['savepath'] . $info['savename'];
            }
        }

        // 文艺团队logo
        if ($postData['group_type'] == 1 && !empty($_FILES["logo"]["tmp_name"])) {
            $info = $this->upload_images($_FILES["logo"], array('jpg', 'png', 'jpeg'), 'group/' . date('Ymd'));
            if (!is_array($info)) {
                $this->error($info);
            } else {
                $postData['logo'] = 'UploadFiles/' . $info['savepath'] . $info['savename'];
            }
        }

        if (isset($_POST['id']) && !empty($_POST['id'])) {
            $id = intval($_POST['id']);
            $result = $this->model->where('id=' . $id)->save($postData);
        } else {
            $postData['create_at'] = date('Y-m-d H:i:s');
            $result = $this->model->add($postData);
        }

        if ($result) {
            $this->success('操作成功.', U('index') . '?type=' . $postData['group_type']);
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 删除文化日历
     */
    public function del()
    {
        $id = intval($_REQUEST['did']);
        $info = $this->model->where('id=' . $id)->find();
        if (!$info) {
            $this->error('信息错误.');
            exit();
        }

        if (intval($info['del']) == 1) {
            $this->success('操作成功！.');
            exit();
        }

        $result = $this->model->where('id=' . $id)->save(['is_del' => 1]);
        if ($result) {
            $this->redirect('index', ['type' => $info['group_type']]);
        } else {
            $this->error('操作失败.');
        }
    }

    /**
     * 预约报名列表
     */
    public function apply()
    {
        $this->assign('title', '报名情况');
        $id = intval($_GET['id']);
        $where = "group_id = " . $id;
        $this->assign('group_id', $id);

        $count = M('group_apply')->where($where)->count();
        $perPage = 20;
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        $totalPage = ceil($count / $perPage);
        $pageObj = $this->page_index($count, $totalPage, $page);
        $list = M('group_apply')->join('lr_group on lr_group.id = lr_group_apply.group_id')->field('lr_group_apply.*, lr_group.title')->where($where)->order('apply_status asc, id asc')->limit($page * $perPage, $perPage)->select();

        $this->assign('list', $list);
        $this->assign('pageObj', $pageObj);
        $this->assign('groupName', M('group')->where('id=' . $id)->getField('title'));
        $this->display();
    }

    /**
     * 预约审核通过
     */
    function applySuc()
    {
        $id = intval($_GET['id']);
        if (empty($id)) {
            $this->error('操作失败.');
        }
        $info = M('group_apply')->where('id=' . $id)->find();
        if (!$info) {
            $this->error('信息错误.');
        }

        if (intval($info['apply_status']) == 2) {
            $this->success('操作成功！.');
        }

        $result = M('group_apply')->where('id=' . $id)->save(['apply_status' => 2, 'update_at' => date('Y-m-d H:i:s')]);

        if ($result) {
            $this->redirect('apply', ['id' => $info['group_id']]);
        } else {
            $this->error('操作失败.');
        }
    }

    /**
     * 预约审核被拒绝
     */
    public function applyFail()
    {
        if (empty($_POST['id']) || empty(($_POST['apply_remark']))) {
            exit(json_encode(array('done' => false, 'msg' => '请输入拒绝原因')));
        }
        $data = [
            'apply_remark' => $_POST['apply_remark'],
            'apply_status' => 3,
            'update_at'    => date('Y-m-d H:i:s')
        ];

        $result = M('group_apply')->where(['id' => intval($_POST['id'])])->save($data);
        if ($result) {
            exit(json_encode(array('done' => true, 'msg' => '回复提交成功')));
        } else {
            exit(json_encode(array('done' => false, 'msg' => '数据保存错误')));
        }
    }
}